package com.xbongbong.pro.statistics.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.bi.commons.pojo.AggFieldPojo;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BiErrorCodeEnum;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.result.pojo.FieldAttrPojo;
import com.xbongbong.pro.statistics.service.CkPreParseResult;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 漏斗图结果集前置处理
 *
 * @author zyy
 * @date 2022/05/09/ 09:03:00
 * @since v1.0
 */
@Service
public class FunnelPreParseResult implements CkPreParseResult {

    private static final Integer chartCode = ChartTypeEnum.FUNNEL.getCode();

    @Override
    public JSONArray preParseResultArray(JSONArray resultArray, ChartEntity chartEntity, Map<String, AggFieldPojo> aggFieldMap) throws XbbException {
        FieldAttrPojo classifyAttrPojo = JSONArray.parseArray(chartEntity.getChartClassify().toJSONString(), FieldAttrPojo.class).get(0);
        //只有分类为复选框的时候才需要执行下面的逻辑
        if(!FieldTypeEnum.isCombox4Funnel(classifyAttrPojo.getFieldType())){
            return resultArray;
        }
        String valurAttr = "";
        JSONArray result = null;
        for (Map.Entry<String, AggFieldPojo> aggFieldPojoEntry : aggFieldMap.entrySet()) {
            valurAttr = aggFieldPojoEntry.getKey();
            //只取一个就行,漏斗图只能设置一个值
            break;
        }
        if(CollectionsUtil.isNotEmpty(resultArray)){
            result = new JSONArray();
            Map<String,Integer> temMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            try {
                for (Object obj : resultArray) {
                    JSONObject jsonObj = obj instanceof JSONObject ? (JSONObject) obj : new JSONObject();
                    JSONArray jsonArray = jsonObj.getJSONArray(classifyAttrPojo.getAttr());
                    if (CollectionsUtil.isEmpty(jsonArray)){
                        continue;
                    }
                    List<String> itemList = jsonArray.toJavaList(String.class);
                    for (String key : itemList) {
                        temMap.put(key,temMap.getOrDefault(key,0) + (Integer) jsonObj.get(valurAttr));
                    }
                }
            }catch (Exception e){
                throw new XbbException(BiErrorCodeEnum.API_ERROR_241024);
            }
            //将数据重新放回去
            for (Map.Entry<String, Integer> entry : temMap.entrySet()) {
                JSONArray array = new JSONArray();
                array.add(entry.getKey());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(classifyAttrPojo.getAttr(),array);
                jsonObject.put(valurAttr,entry.getValue());
                jsonObject.put("del",0);
                result.add(jsonObject);
            }
        }
        return result == null ? resultArray : result;
    }

    @Override
    public Integer getCode() {
        return chartCode;
    }
}
